package com.andnnl.secure;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

/**
 * Created by chenss on 2020/12/14.
 */
public class SecureTest {
    public static void main(String[] args) {
//        destest();
        aesTest();
    }

    private static void aesTest() {
        String content = "test中文askdlfj卢卡斯大家发柘城苛夺 苛夺苛夺";

// 随机生成密钥
//        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        byte[] key = "asdfkjlx12345678".getBytes();
        System.out.println(key.length);
        System.out.println(SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).toString());
// 构建
        AES aes = SecureUtil.aes(key);

// 加密
        byte[] encrypt = aes.encrypt(content);
// 解密
        byte[] decrypt = aes.decrypt(encrypt);

// 加密为16进制表示
        String encryptHex = aes.encryptHex(content);
        System.out.println(encryptHex);
// 解密为字符串
        String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
        System.out.println(decryptStr);
        System.out.println(decryptStr.equals(content));
    }

    private static void destest() {
        String content = "test中文askdlfj卢卡斯大家发柘城苛夺 苛夺苛夺";

        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
        System.out.println(new String(key));
        SymmetricCrypto des = new SymmetricCrypto(SymmetricAlgorithm.DESede, key);

//加密
        byte[] encrypt = des.encrypt(content);
//解密
        byte[] decrypt = des.decrypt(encrypt);

//加密为16进制字符串（Hex表示）
        String encryptHex = des.encryptHex(content);
        System.out.println(encryptHex);
//解密为字符串
        String decryptStr = des.decryptStr(encryptHex);
        System.out.println(decryptStr);
    }
}
